//
//  PaimingMonthDetailViewController.m
//  jizhiyunduan
//
//  Created by 鞠超 on 16/4/25.
//  Copyright © 2016年 JC. All rights reserved.
//

#import "PaimingMonthDetailViewController.h"

#import "PNChartDelegate.h"
#import "PNChart.h"

@interface PaimingMonthDetailViewController () <UITableViewDelegate, UITableViewDataSource, PNChartDelegate>

@property (nonatomic, strong) UITableView * tableView;
//头视图
@property (nonatomic, strong) UIView * headView;
@property (nonatomic) PNLineChart * lineChart1;
//是否是首次
@property (nonatomic, assign) NSInteger fisrt_fresh1;

@property (nonatomic, strong) UIView * view_head;
//数据
@property (nonatomic, assign) NSInteger number;

@end

@implementation PaimingMonthDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void)p_navi
{
    //11个字
    _lblTitle.text = [NSString stringWithFormat:@"日排名详情"];
    
    _btnRight.hidden = YES;
    
    [self addLeftButton:@"fanhui@2x.png"];
}

//左返回
- (void)clickLeftButton:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//隐藏tabbar
-(void)viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void)p_setupView
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    
    [self p_headView];
    self.tableView.tableHeaderView = self.headView;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];
    
    //刷新
    __unsafe_unretained UITableView *tableView = self.tableView;
    __unsafe_unretained __typeof(self) weakSelf = self;
    
    // 下拉刷新
    tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [weakSelf p_data];
        // 结束刷新
        [tableView.mj_header endRefreshing];
    }];
    
    // 马上进入刷新状态
    [tableView.mj_header beginRefreshing];
    
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    tableView.mj_header.automaticallyChangeAlpha = YES;
    
//    // 上拉加载
//    tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
//
//        [weakSelf p_data1];
//        // 结束刷新
//        [tableView.mj_footer endRefreshing];
//    }];
}

#pragma mark - 数据
- (void )p_data
{
    self.number = 30;
    
    if(self.fisrt_fresh1 == 0)
    {
        self.fisrt_fresh1 = 1;
    }
    else
    {
        self.lineChart1.displayAnimated = YES;
        [self.lineChart1 strokeChart];
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        [self.tableView reloadData];
    });
}

#pragma mark - tableview代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.number;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [[UITableViewCell alloc] init];
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    CGFloat list_x = (SCREEN_WIDTH - 10) / 4;
    
    //需要空2格
    UILabel * lbl_1 = [[UILabel alloc] initWithFrame:CGRectMake(5 , 7.5, list_x * 2, 30)];
    lbl_1.text = [NSString stringWithFormat:@"  网站关键词%d",indexPath.row + 1];
    lbl_1.textColor = [UIColor blackColor];
    lbl_1.font = [UIFont systemFontOfSize:13];
    [cell addSubview:lbl_1];
    
    UILabel * lbl_2 = [[UILabel alloc] initWithFrame:CGRectMake(5 + list_x * 2, 7.5, list_x, 30)];
    lbl_2.text = [NSString stringWithFormat:@"%d",(indexPath.row + 1) * 2];
    lbl_2.textColor = [UIColor blackColor];
    lbl_2.textAlignment = NSTextAlignmentCenter;
    lbl_2.font = [UIFont systemFontOfSize:15];
    [cell addSubview:lbl_2];
    
    UILabel * lbl_3 = [[UILabel alloc] initWithFrame:CGRectMake(5 + list_x * 3, 7.5, list_x * 1, 30)];
    lbl_3.text = [NSString stringWithFormat:@"12-%d",indexPath.row + 1];
    lbl_3.textColor = [UIColor blackColor];
    lbl_3.textAlignment = NSTextAlignmentCenter;
    lbl_3.font = [UIFont systemFontOfSize:15];
    [cell addSubview:lbl_3];
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    self.view_head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
    self.view_head.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    CGFloat list_x = (SCREEN_WIDTH - 10) / 4;
    
    //1
    UILabel * list_1 = [[UILabel alloc] initWithFrame:CGRectMake(5 , 7.5, list_x * 2, 20)];
    list_1.text = @"  关键词";
    list_1.textColor = navi_bar_bg_color;
    list_1.font = [UIFont systemFontOfSize:13];
    //    list_1.textAlignment = NSTextAlignmentCenter;
    [self.view_head addSubview:list_1];
    
    //2
    UILabel * list_2 = [[UILabel alloc] initWithFrame:CGRectMake(5 + list_x * 2, 7.5, list_x * 1, 20)];
    list_2.text = @"排名";
    list_2.textColor = navi_bar_bg_color;
    list_2.font = [UIFont systemFontOfSize:13];
    list_2.textAlignment = NSTextAlignmentCenter;
    [self.view_head addSubview:list_2];
    
    //3
    UILabel * list_3 = [[UILabel alloc] initWithFrame:CGRectMake(5 + list_x * 3, 7.5, list_x * 1, 20)];
    list_3.text = @"日期";
    list_3.textColor = navi_bar_bg_color;
    list_3.font = [UIFont systemFontOfSize:13];
    list_3.textAlignment = NSTextAlignmentCenter;
    [self.view_head addSubview:list_3];
    
    return self.view_head;
}

- (CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 35.0f;
}

#pragma mark - 头视图
- (void )p_headView
{
    self.fisrt_fresh1 = 0;
    
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
    self.headView.backgroundColor = [UIColor whiteColor];
    
    
    self.lineChart1 = [[PNLineChart alloc] initWithFrame:CGRectMake(10, 15, SCREEN_WIDTH - 20, 270)];
    self.lineChart1.yLabelFormat = @"%d";
    self.lineChart1.backgroundColor = [UIColor clearColor];
    [self.headView addSubview:self.lineChart1];
    
    //1个月数据 x轴
    [self.lineChart1 setXLabels:@[@"12.1",@"12.6",@"12.11",@"12.16",@"12.21",@"12.26",@"12.31"]];
    //是否显示 x轴
    self.lineChart1.showCoordinateAxis = YES;
    
    self.lineChart1.yFixedValueMax = 1;
    self.lineChart1.yFixedValueMin = 100;
    
    self.lineChart1.yLabelFont = [UIFont systemFontOfSize:10];
    [self.lineChart1 setYLabels:@[
                                  @"100",
                                  @"90",
                                  @"80",
                                  @"70",
                                  @"60",
                                  @"50",
                                  @"40",
                                  @"30",
                                  @"20",
                                  @"10",
                                  @"1",
                                  ]
     ];
    
    //线
    NSArray * data01Array = @[@33, @86, @26, @97, @6, @12, @56];
    PNLineChartData *data01 = [PNLineChartData new];
    data01.dataTitle = @"5天平均排名";
    data01.color = [UIColor brownColor];
    data01.alpha = 0.3f;
    data01.itemCount = data01Array.count;
    data01.inflexionPointStyle = PNLineChartPointStyleTriangle;
    data01.getData = ^(NSUInteger index) {
        CGFloat yValue = [data01Array[index] floatValue];
        return [PNLineChartDataItem dataItemWithY:yValue];
    };
    
    self.lineChart1.chartData = @[data01];
    [self.lineChart1 strokeChart];
    self.lineChart1.delegate = self;
    
    
    self.lineChart1.legendStyle = PNLegendItemStyleStacked;
    self.lineChart1.legendFont = [UIFont boldSystemFontOfSize:12.0f];
    self.lineChart1.legendFontColor = [UIColor brownColor];
    
    UIView *legend = [self.lineChart1 getLegendWithMaxWidth:200];
    [legend setFrame:CGRectMake(50, 10, 200, 30)];
    [self.lineChart1 addSubview:legend];
}

#pragma mark - 点击方法
- (void)userClickedOnLineKeyPoint:(CGPoint)point lineIndex:(NSInteger)lineIndex pointIndex:(NSInteger)pointIndex
{
    
}

- (void)userClickedOnLinePoint:(CGPoint)point lineIndex:(NSInteger)lineIndex
{
    
}


@end
